//数据库信息
// 1.安装sequlize mysql
const {Sequelize, DataTypes} = require('sequelize');//导包
const { FOREIGNKEYS } = require('sequelize/lib/query-types');
const sequelize = new Sequelize('traveldb','root','root',{//建立连接
    host: 'localhost',
    dialect:'mysql',
    dialectOptions:{charset:"utf8mb4"},
    'define':{
        'underscored':true,
        'charset':'utf8mb4',
        'freezeTableName':true
    },
    pool: {
        max: 5, // 连接池的最大连接数量
        min: 0, // 连接池的最小连接数量
        idle: 10000 // 如果一个线程10秒钟内没有被使用，那么就释放线程
      }
});

// 导入模型
const UserModel = require('./model/user')(sequelize,DataTypes);
const articleModel = require('./model/article')(sequelize,DataTypes);
const CommentModel = require('./model/comment')(sequelize,DataTypes);
const TicketModel = require('./model/ticket')(sequelize,DataTypes);
const OderModel = require('./model/order')(sequelize,DataTypes);
const TravelModel = require('./model/travel')(sequelize,DataTypes);


// 写关系
// 一对多 用户跟文章/评论/门票/订单  
UserModel.hasMany(articleModel,{FOREIGNKEYS:'uId'});
UserModel.hasMany(CommentModel,{FOREIGNKEYS:'uId'});
UserModel.hasMany(TicketModel,{FOREIGNKEYS:'tickId'})
UserModel.hasMany(OderModel,{FOREIGNKEYS:'oId'})
//一对多 文章跟评论  
articleModel.hasMany(CommentModel,{FOREIGNKEYS:'conId'})
//一对多 景点跟文章/门票
TravelModel.hasMany(articleModel,{FOREIGNKEYS:'conId'})
TravelModel.hasMany(TicketModel,{FOREIGNKEYS:'tickId'})

// 多对多 门票 跟订单
// TicketModel.belongsTo(OderModel,{FOREIGNKEYS:''})

// 导出
module.exports ={
    sequelize,
    UserModel,
    articleModel,
    CommentModel,
    OderModel,
    TicketModel,
    TravelModel
}
